package com.seezoon.application.device.executor;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.seezoon.application.device.dto.FencePageQry;
import com.seezoon.application.device.dto.clientobject.FenceCO;
import com.seezoon.domain.dao.mapper.UserDeviceFenceMapper;
import com.seezoon.domain.dao.po.UserDeviceFencePO;
import com.seezoon.infrastructure.configuration.context.SecurityContextHolder;
import com.seezoon.infrastructure.dto.Page;
import com.seezoon.infrastructure.dto.Response;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

/**
 * 分页查询围栏
 */
@RequiredArgsConstructor
@Slf4j
@Component
@Validated
public class FencePageQryExe {

    private final UserDeviceFenceMapper userDeviceFenceMapper;

    public Response<Page<FenceCO>> execute(@Valid @NotNull FencePageQry qry) {
        Long uid = SecurityContextHolder.getUid();
        PageHelper.startPage(qry.getPage(), qry.getPageSize());
        PageSerializable<UserDeviceFencePO> page = new PageSerializable(
                userDeviceFenceMapper.selectByUidAndDeviceId(uid, qry.getDeviceId()));
        List<FenceCO> vos = new ArrayList<>();
        page.getList().forEach(po -> {
            FenceCO co = new FenceCO();
            co.setId(po.getId());
            co.setFenceName(po.getFenceName());
            co.setDeviceId(po.getDeviceId());
            co.setCenterLatitude(po.getCenterLatitude());
            co.setCenterLongitude(po.getCenterLongitude());
            co.setRadius(po.getRadius());
            co.setStatus(po.getStatus());
            co.setCreateTime(po.getCreateTime());
            co.setUpdateTime(po.getUpdateTime());
        });
        return Response.success(new Page<>(page.getTotal(), vos));
    }
}
